package com.zhm.demo.http;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.Router;

public class VertxTest extends AbstractVerticle {

    @Override
    public void start() throws Exception {
        System.out.println("begin");
        // 创建Router实例
        Router router = Router.router(vertx);

        // 定义一个路由，当访问根路径("/")时，返回"Hello, Vert.x!"
        router.get("/").handler(routingContext -> {
            HttpServerRequest request = routingContext.request();
            request.response()
                    .putHeader("content-type", "text/plain")
                    .end("Hello, Vert.x!");
        });

        // 创建HTTP服务器，并监听8080端口
        HttpServer server = vertx.createHttpServer();
        server.listen(8083, ar -> {
            if (ar.succeeded()) {
                System.out.println("HTTP server is now running!");
            } else {
                System.out.println("Failed to start the server: " + ar.cause().getMessage());
            }
        });
        System.out.println("done");
    }

    public static void main(String[] args) {
        System.out.println("begin 2");
        // 创建Vertx实例
        Vertx vertx = Vertx.vertx();

        // 部署Verticle
        vertx.deployVerticle(new VertxTest());
    }
}